package cn.nebula.music.service;

import java.sql.Date;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.nebula.music.dao.CombinationSingerDAO;
import cn.nebula.music.dao.PictureDAO;
import cn.nebula.music.dao.SingerAlbumDAO;
import cn.nebula.music.dao.SingerClassificationDAO;
import cn.nebula.music.dao.SingerDAO;
import cn.nebula.music.dao.SingerSongDAO;
import cn.nebula.music.dao.UserSingerDAO;
import cn.nebula.music.domain.Album;
import cn.nebula.music.domain.Classification;
import cn.nebula.music.domain.Combination;
import cn.nebula.music.domain.CombinationSinger;
import cn.nebula.music.domain.Singer;
import cn.nebula.music.domain.SingerAlbum;
import cn.nebula.music.domain.SingerClassification;
import cn.nebula.music.domain.SingerSong;
import cn.nebula.music.domain.Song;
import cn.nebula.music.domain.User;
import cn.nebula.music.domain.UserSinger;

@Service
public class SingerServiceImpl implements SingerService {

	@Autowired
	private SingerDAO singerDAO;

	@Autowired
	private SingerAlbumDAO singerAlbumDAO;

	@Autowired
	private SingerClassificationDAO singerClassificationDAO;

	@Autowired
	private SingerSongDAO singerSongDAO;

	@Autowired
	private UserSingerDAO userSingerDAO;

	@Autowired
	private CombinationSingerDAO combinationSingerDAO;

	@Autowired
	private PictureDAO pictureDAO;

	@Override
	public Set<Singer> findAllSingers() throws Exception {
		return singerDAO.findAllSingers();
	}

	@Override
	public Set<Singer> findAllSingersOfBT() throws Exception {
		return singerDAO.findAllSingersOfBT();
	}

	@Override
	public Set<Singer> findAllSingersWP() throws Exception {
		return singerDAO.findAllSingersWP();
	}

	@Override
	public Singer findSingerById(Integer id) throws Exception {
		return singerDAO.findSingerById(id);
	}

	@Override
	public Set<Singer> findSingersByChinesename(String chinesename) throws Exception {
		return singerDAO.findSingersByChinesename(chinesename);
	}

	@Override
	public Set<Singer> findSingersByForeignname(String foreignname) throws Exception {
		return singerDAO.findSingersByForeignname(foreignname);
	}

	@Override
	public Set<Singer> findSingersByAlias(String alias) throws Exception {
		return singerDAO.findSingersByAlias(alias);
	}

	@Override
	public Set<Singer> findSingersByConstellation(String constellation) throws Exception {
		return singerDAO.findSingersByConstellation(constellation);
	}

	@Override
	public Set<Singer> findSingersByBirthday(Date birthday) throws Exception {
		return singerDAO.findSingersByBirthday(birthday);
	}

	@Override
	public Set<Singer> findSingersByHeat(Integer heat) throws Exception {
		return singerDAO.findSingersByHeat(heat);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void addSinger(Singer singer) throws Exception {
		singerDAO.addSinger(singer);
		Set<Album> albums = singer.getAlbums();
		if (albums != null) {
			for (Album album : albums) {
				SingerAlbum sa = new SingerAlbum();
				sa.setSinger(singer.getId());
				sa.setAlbum(album.getId());
				singerAlbumDAO.addSingerAlbum(sa);
			}
		}
		Set<Song> songs = singer.getSongs();
		if (songs != null) {
			for (Song song : songs) {
				SingerSong ss = new SingerSong();
				ss.setSinger(singer.getId());
				ss.setSong(song.getId());
				singerSongDAO.addSingerSong(ss);
			}
		}
		Set<Classification> classifications = singer.getClassifications();
		if (classifications != null) {
			for (Classification classification : classifications) {
				SingerClassification sc = new SingerClassification();
				sc.setSinger(singer.getId());
				sc.setClassification(classification.getId());
				singerClassificationDAO.addSingerClassification(sc);
			}
		}
		Set<User> users = singer.getUsers();
		if (users != null) {
			for (User user : users) {
				UserSinger us = new UserSinger();
				us.setSinger(singer.getId());
				us.setUser(user.getId());
				userSingerDAO.addUserSinger(us);
			}
		}
		Set<Combination> combinations = singer.getCombinations();
		if (users != null) {
			for (Combination combination : combinations) {
				CombinationSinger cs = new CombinationSinger();
				cs.setSinger(singer.getId());
				cs.setCombination(combination.getId());
				combinationSingerDAO.addCombinationSinger(cs);
			}
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void updateSinger(Singer singer) throws Exception {
		singerDAO.updateSinger(singer);
		Boolean isModify = false;
		int size1 = 0;// 更改前的数量
		int size2 = 0;// 更改后的数量
		Set<User> users1 = findSingerById(singer.getId()).getUsers();
		Set<User> users2 = singer.getUsers();
		if (users1 != null) {
			size1 = users1.size();
		}
		if (users2 != null) {
			size2 = users2.size();
		}
		if (size1 == size2) {
			if (!users1.toString().equals(users2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (User user : users1) {
				UserSinger us = new UserSinger();
				us.setSinger(singer.getId());
				us.setUser(user.getId());
				userSingerDAO.deleteUserSinger(us);
			}
			for (User user : users2) {
				UserSinger us = new UserSinger();
				us.setSinger(singer.getId());
				us.setUser(user.getId());
				userSingerDAO.addUserSinger(us);
			}
		}
		isModify = false;
		size1 = 0;// 更改前的数量
		size2 = 0;// 更改后的数量
		Set<Album> albums1 = findSingerById(singer.getId()).getAlbums();
		Set<Album> albums2 = singer.getAlbums();
		if (albums1 != null) {
			size1 = albums1.size();
		}
		if (albums2 != null) {
			size2 = albums2.size();
		}
		if (size1 == size2) {
			if (!albums1.toString().equals(albums2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (Album album : albums1) {
				SingerAlbum sa = new SingerAlbum();
				sa.setSinger(singer.getId());
				sa.setAlbum(album.getId());
				singerAlbumDAO.deleteSingerAlbum(sa);
			}
			for (Album album : albums2) {
				SingerAlbum sa = new SingerAlbum();
				sa.setSinger(singer.getId());
				sa.setAlbum(album.getId());
				singerAlbumDAO.addSingerAlbum(sa);
			}
		}
		isModify = false;
		size1 = 0;// 更改前的数量
		size2 = 0;// 更改后的数量
		Set<Song> songs1 = findSingerById(singer.getId()).getSongs();
		Set<Song> songs2 = singer.getSongs();
		if (songs1 != null) {
			size1 = songs1.size();
		}
		if (songs2 != null) {
			size2 = songs2.size();
		}
		if (size1 == size2) {
			if (!songs1.toString().equals(songs2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (Song song : songs1) {
				SingerSong ss = new SingerSong();
				ss.setSinger(singer.getId());
				ss.setSong(song.getId());
				singerSongDAO.deleteSingerSong(ss);
			}
			for (Song song : songs2) {
				SingerSong ss = new SingerSong();
				ss.setSinger(singer.getId());
				ss.setSong(song.getId());
				singerSongDAO.addSingerSong(ss);
			}
		}
		isModify = false;
		size1 = 0;// 更改前的数量
		size2 = 0;// 更改后的数量
		Set<Classification> classifications1 = findSingerById(singer.getId()).getClassifications();
		Set<Classification> classifications2 = singer.getClassifications();
		if (classifications1 != null) {
			size1 = classifications1.size();
		}
		if (classifications2 != null) {
			size2 = classifications2.size();
		}
		if (size1 == size2) {
			if (!classifications1.toString().equals(classifications2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (Classification classification : classifications1) {
				SingerClassification sc = new SingerClassification();
				sc.setSinger(singer.getId());
				sc.setClassification(classification.getId());
				singerClassificationDAO.deleteSingerClassificationBySingerId(singer.getId());
			}
			for (Classification classification : classifications2) {
				SingerClassification sc = new SingerClassification();
				sc.setSinger(singer.getId());
				sc.setClassification(classification.getId());
				singerClassificationDAO.addSingerClassification(sc);
			}
		}
		isModify = false;
		size1 = 0;// 更改前的数量
		size2 = 0;// 更改后的数量
		Set<Combination> combinations1 = findSingerById(singer.getId()).getCombinations();
		Set<Combination> combinations2 = singer.getCombinations();
		if (combinations1 != null) {
			size1 = combinations1.size();
		}
		if (combinations2 != null) {
			size2 = combinations2.size();
		}
		if (size1 == size2) {
			if (!combinations1.toString().equals(combinations2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (Combination combination : combinations1) {
				CombinationSinger cs = new CombinationSinger();
				cs.setSinger(singer.getId());
				cs.setCombination(combination.getId());
				combinationSingerDAO.deleteCombinationSinger(cs);
			}
			for (Combination combination : combinations2) {
				CombinationSinger cs = new CombinationSinger();
				cs.setSinger(singer.getId());
				cs.setCombination(combination.getId());
				combinationSingerDAO.addCombinationSinger(cs);
			}
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void deleteSingerById(Integer id) throws Exception {
		singerAlbumDAO.deleteSingerAlbumBySingerId(id);
		singerSongDAO.deleteSingerSongBySingerId(id);
		singerClassificationDAO.deleteSingerClassificationBySingerId(id);
		userSingerDAO.deleteUserSingerBySingerId(id);
		combinationSingerDAO.deleteCombinationSingerBySingerId(id);
		singerDAO.deleteSingerById(id);
	}

}
